'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        		function($scope, $rootScope, $state, $timeout) {
    	var title = "";
        //var defaultAva = $rootScope.defaultAvatar;
        //$scope.myImage = '';
        //$scope.myCroppedImage = '';
         getselectActivity();
         getselectRoute();
        if ($state.includes('**.task.update')) {
            title = "修改任务";
            var id = $state.params.id;
            validate();
            detail(id);
        } else if ($state.includes('**.task.create')) {
            title = "添加任务";
            validate();
        }
        
        $scope.title = $rootScope.title = title;
        $scope.loading = true;
                     function getselectActivity(){
                         $scope.loading = true;
                            $.ajax({
                                url : $scope.app.project_name+ '/activity/read/selectActivity',
                                data:{"status":"0"}
                            }).then(function(result) {
                                $scope.loading = false;
                                if (result.httpCode == 200) {
                                    $scope.activityList = result.data;
                                } else {
                                    $scope.msg = result.msg;
                                }
                                $scope.$apply();
                            });
                        }
                        function getselectRoute(){
                            $scope.loading = true;
                            $.ajax({
                                url : $scope.app.project_name+ '/route/read/selectRoute'
                            }).then(function(result) {
                                $scope.loading = false;
                                if (result.httpCode == 200) {
                                    $scope.routeList = result.data;
                                } else {
                                    $scope.msg = result.msg;
                                }
                                $scope.$apply();
                            });
                        }
                    function validate() {
                        jQuery("form").validate({
                            rules: {
                                activityId: {
                                    required: true
                                },
                                routeId: {
                                    required: true
                                },
                                taskName: {
                                    required: true,
                                    maxlength:10
                                },
                                costTime: {
                                    required: true,
                                    min:1,
                                    maxlength:4,
                                    number:true
                                },
                                sort: {
                                    required: true,
                                    maxlength: 3,
                                    min:1,
                                    number:true
                                },
                                taskDesc: {
                                    required: true,
                                    maxlength:200
                                },finishTime:{
                                  required:true
                                },
                                destroyBy: {
                                    required: true,
                                     min:1,
                                    maxlength:9,
                                    number:true
                                }
                            },
                            messages: {
                                activityId: {
                                    required: '活动不能为空'
                                },
                                routeId: {
                                    required: '路线不能为空'
                                },
                                taskName:{
                                    required: '任务名称不能为空',
                                    maxlength: '任务名称长度不可超过10'
                                }, costTime: {
                                    required: '时间限制不能为空',
                                    min:'时间限制最小值为1',
                                    maxlength:'时间限制不可大于4位',
                                    number:'请输入数字'
                                }, sort: {
                                    required: '所在层级不能为空',
                                    maxlength: '层级长度不可大于3位',
                                    min:'层级最小值为1',
                                    number:'请输入数字'
                                }, taskDesc: {
                                    required: '任务描述不能为空',
                                    maxlength: '任务名称长度不可超过200'
                                }, destroyBy: {
                                    required: '核销人不能为空',
                                    min:'核销者ID最小值为1',
                                    maxlength:'核销者ID不可超过9位',
                                    number:'请输入数字'
                                },finishTime:{
                                        required:'任务截止日期不能为空'
                                }
                            },
                            submitHandler: function () {
                                $scope.submit();
                            }
                        });
                    }
                    $scope.submit=function(){
                        $scope.record.finishTime = $('#finishTime').val();
                        var m = $scope.record;
                        console.log($scope.record);
                        if(m){
                            $scope.isDisabled = true;//提交disabled
                            $.ajax({
                                url : $scope.record.taskId? $scope.app.project_name + '/task/update' : $scope.app.project_name + '/task/add',
                                data: $scope.record
                            }).then(callback);
                        }

                        function callback(result){
                            console.log(result);
                            if(result.httpCode ==200){//成功
                                if(result.data==200){
                                    $timeout(function(){
                                        $state.go('main.funScenes.task.list');
                                    },2000);
                                }else if(result.data==1001){
                                    $scope.alertModal({"content":"该路线下的任务时限超出活动总时间，请修改"});
                                    $scope.isDisabled = false;
                                }else if(result.data==1002){
                                    $scope.alertModal({"content":"所属层级已存在，请更改"});
                                    $scope.isDisabled = false;
                                }
                            }else{
                                alert("请求失败");
                                $scope.isDisabled = true;
                            }
                            $scope.loading = false;
                        }
                    }

        // 初始化页面
        function detail(id) {
	        $scope.loading = true;
        	$.ajax({ 
				url : $scope.app.project_name + '/task/read/detail',
				data: {'id': id}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
				   console.log(result.data);
					$scope.record = result.data;
					if(result.data.taskStatus==2){
                    $("#activityId").attr("disabled",true);
                        $("#routeId").attr("disabled",true);
                    }
                    /*var actId=$scope.record.activityId;
                    $scope.record.activityStarttime=actId;
                    $scope.record.activityEndtime=actId;*/
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }
       /* $scope.changeAct=function(){
            var actId=$scope.record.activityId;
            $scope.record.activityStarttime=actId;
            $scope.record.activityEndtime=actId;
        }*/

}]);
